import Vue from 'vue'
import VueRouter from 'vue-router'
// -------------------------------------------------登录组件
import Login from '../components/Login.vue'
// -------------------------------------------------主页组件
import Home from '../components/Home.vue'
// -------------------------------------------------欢迎组件
import Welcome from '../components/Welcome.vue'
// -------------------------------------------------用户列表组件
import Users from '../components/user/Users.vue'
// -------------------------------------------------用户权限组件
import Rights from '../components/power/Rights.vue'
// -------------------------------------------------角色管理组件
import Roles from '../components/power/Roles.vue'
// -------------------------------------------------商品分类组件
import Cate from '../components/goods/Cate.vue'
// -------------------------------------------------分类参数组件
import Params from '../components/goods/Params.vue'
// -------------------------------------------------商品列表组件
import List from '../components/goods/List.vue'
// -------------------------------------------------添加商品组件
import Add from '../components/goods/Add.vue'
// -------------------------------------------------订单列表组件
import Order from '../components/order/Order.vue'
// -------------------------------------------------数据报表组件
import Report from '../components/report/Report.vue'
// -------------------------------------------------用户列表练习组件
import UsersLianXi from '../components/user/Users_lianxi.vue'

Vue.use(VueRouter)

const routes = [
  // ---------访问跟组件时,重定向为登录页面---------
  { path: '/', redirect: '/login' },
  // ---------登录组件路由地址---------
  { path: '/login', component: Login },
  // ---------主页组件路由地址---------
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      // ---------欢迎组件路由地址---------
      { path: '/welcome', component: Welcome },
      // ---------用户列表组件路由地址---------
      { path: '/users', component: Users },
      // ---------用户权限组件路由地址---------
      { path: '/rights', component: Rights },
      // ---------角色管理组件路由地址---------
      { path: '/roles', component: Roles },
      // ---------商品分类组件路由地址---------
      { path: '/categories', component: Cate },
      // ---------分类参数组件路由地址---------
      { path: '/params', component: Params },
      // ---------商品列表组件路由地址---------
      { path: '/goods', component: List },
      // ---------添加商品组件路由地址---------
      { path: '/goods/add', component: Add },
      // ---------订单列表组件路由地址---------
      { path: '/orders', component: Order },
      // ---------数据报表组件路由地址---------
      { path: '/reports', component: Report },
      // ---------用户列表练习组件路由地址---------
      { path: '/lianxi', component: UsersLianXi }

    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫 (控制访问权限)
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数, 表示放行
  // next() 放行   next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenSrt = window.sessionStorage.getItem('token')
  // 如果没有 token,代表没有登录,强制跳转到登录页
  if (!tokenSrt) return next('/login')
  next()
})

export default router
